import os
import requests

def call_api(question, answer):
    if isinstance(answer, str):
        answer = [answer]
    retrival_res = requests.post(
        url='https://dashscope.aliyuncs.com/api/v1/services/rerank/text-rerank/text-rerank',
        headers={
            'Content-Type': 'application/json',
            'Authorization': 'Bearer %s' % os.environ.get('DASHSCOPE_API_KEY')
        },
        json={
            "model": "qwen3-rerank",
            "input": {
                "query": question,
                "documents": answer
            },
            "parameters": {
                "instruct": "Given a web search query, retrieve relevant passages that answer the query."
            }
        }
    )
    retrival_res = retrival_res.json()['output']['results']
    '''example return
    [
        {
            "document": {
                "text": "文本排序模型广泛用于搜索引擎和推荐系统中，它们根据文本相关性对候选文本进行排序"
            },
            "index": 0,
            "relevance_score": 0.9334521178273196
        },
        {
            "document": {
                "text": "预训练语言模型的发展给文本排序模型带来了新的进展"
            },
            "index": 2,
            "relevance_score": 0.34100082626411193
        }
    ]
    '''
    return retrival_res
